//
//  ViewController.m
//  20-Homework XML解析
//
//  Created by GivenBmli on 16/8/10.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ViewController.h"
#import "Cartoon.h"



@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong)NSMutableArray *stuArray;
@property (nonatomic, copy)NSString *currentElementName;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}
- (IBAction)saxParse:(id)sender {
    NSString *xmlDataSting = [[NSBundle mainBundle] pathForResource:@"Cartoon" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlDataSting];
    NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithData:xmlData];
    xmlParser.delegate = self;
    [xmlParser parse];

}

- (void)parserDidStartDocument:(NSXMLParser *)parser {
    self.stuArray = [NSMutableArray array];
    
}

- (void)parser:(NSXMLParser *)parser didStartElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(nonnull NSDictionary<NSString *,NSString *> *)attributeDict {
    _currentElementName = elementName;
    if ([elementName isEqualToString:@"student"]) {
        Cartoon *cartoon = [Cartoon new];
        [_stuArray addObject:cartoon];
    }

    
   }

- (void)parser:(NSXMLParser *)parser foundCharacters:(nonnull NSString *)string {
    if (_currentElementName) {
        Cartoon *cartoon = [_stuArray lastObject];
        [cartoon setValue:string forKey:_currentElementName];
        NSLog(@"节点的值:  %@  - 节点值结束", string);
    }

   
}

- (void)parser:(NSXMLParser *)parser didEndElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName {
    NSLog(@"结束----:%@",elementName);
    _currentElementName = nil;
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"%@",_stuArray);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
